﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
//using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace tsgl
{
    public partial class userPermission : Form
    {
        public userPermission()
        {
            InitializeComponent();
        }

       
        private void userPermission_Load(object sender, EventArgs e)
        {
            DataTable dtUserType = bookDbClass.ExecuteDataTable("select 用户类型名称 from 用户类型");
            for (int i = 0; i < dtUserType.Rows.Count; i++)
            {
                lbUserType.Items.Add(dtUserType.Rows[i][0]);
            }
            lbUserType.SelectedIndex = 0;
            DataTable dtPermissionListAll = bookDbClass.ExecuteDataTable("select 权限选项名称 from 用户权限选项");
            for (int i = 0; i < dtPermissionListAll.Rows.Count; i++)
            {
                userPermissionList.Items.Add(dtPermissionListAll.Rows[i][0]);
            }
            if (lbUserType.Items.Count != 0)
            {
                setUserPermission(lbUserType.Items[lbUserType.SelectedIndex].ToString());
            }
        }

        public Boolean flag = false;
        private void setUserPermission(string permissionType)
        {
            DataTable dtPermissionList = bookDbClass.ExecuteDataTable("select 用户权限选项 from 用户权限 where 用户类型名称 = @str", new SqlParameter("str", permissionType));
            for (int i = 0; i < userPermissionList.Items.Count; i++)
            {
                userPermissionList.SetItemCheckState(i, CheckState.Unchecked);
            }
            if (userPermissionList.Items.Count != 0)
            {
                for (int i = 0; i < dtPermissionList.Rows.Count; i++)
                {
                    string setOption = dtPermissionList.Rows[i]["用户权限选项"].ToString();
                    for (int j = 0; j < userPermissionList.Items.Count; j++)
                    {
                        string listOption = userPermissionList.Items[j].ToString();
                        if (setOption.Trim() == listOption.Trim())
                        {
                            userPermissionList.SetItemCheckState(j, CheckState.Checked);
                        }
                    }
                }
            }
            flag = true;
        }

            
        private void lbUserType_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                for (int i = 0; i <= this.userPermissionList.Items.Count - 1; i++)
                {
                    userPermissionList.SetItemCheckState(i,CheckState.Checked);
                    //this.userPermissionList_SelectedIndexChanged();
                }
                checkBox2.Checked = false;
            }
        }

        private void userPermissionList_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                bookDbClass.userPermissionDelete(lbUserType.Items[lbUserType.SelectedIndex].ToString());
                int nums = userPermissionList.CheckedItems.Count;
                if (nums != 0)
                {
                    for (int i = 0; i <= nums - 1; i++)
                    {
                        bookDbClass.userPermissionAdd(lbUserType.Items[lbUserType.SelectedIndex].ToString(), userPermissionList.CheckedItems[i].ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("出现错误：" + ex.Message);
            }
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox2.Checked == true)
            {
                for (int i = 0; i <= userPermissionList.Items.Count - 1; i++)
                {
                    userPermissionList.SetItemCheckState(i,CheckState.Unchecked);
                }
                checkBox1.Checked = false;
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void lbUserType_Click(object sender, EventArgs e)
        {
            setUserPermission(lbUserType.Items[lbUserType.SelectedIndex].ToString());
            checkBox1.Checked = false;
            checkBox2.Checked = false;
        }
    }
}